﻿using System;
using System.Collections.Generic;
using System.Text;
using SharpHomeEngine;
using System.Threading;
using System.Reflection;

namespace DeviceInterfaceTest
{
    public class TestDeviceInterface : SharpHomeEngine.DeviceInterface
    {

        public Queue<Request4TagStateChange> Requests = new Queue<Request4TagStateChange>();

        public TestDeviceInterface(StateEngine Engine, String Name)
            : base(Engine, Name)
        {
            this.InterfaceType = "TestDeviceInterface";


        }

        Thread Worker;
        public override void Start()
        {
            Worker = new Thread(new ThreadStart(this.Go));
            Worker.Start();
        }

        private void Go()
        {
            String[] Keys = new String[DeviceDictionary.Keys.Count];
            DeviceDictionary.Keys.CopyTo(Keys, 0);

            while (true)
            {
                // a simulation of a paerson going around and turning the lights on and off in a unsolicited (by the system) manner.
                Random Gen = new Random();
                System.Threading.Thread.Sleep(Gen.Next(300, 1000));
                int X = Gen.Next(0, Keys.Length);
                if (X >= Keys.Length)
                {
                    continue;
                }

                String Address = Keys[X];
                Device Device = DeviceDictionary[Address];
                if (Device is TestDimmerSwitch)
                {
                    TestDimmerSwitch Dimmer = (TestDimmerSwitch)Device;
                    X = Gen.Next(1, 3);
                    if (X == 1)
                    {
                        X = Gen.Next(1, 3);
                        if (X == 1)
                        {
                            UnsolicitedTagStateChange(Dimmer, Dimmer.Switch, Bit.Off);
                        }
                        else
                        {
                            UnsolicitedTagStateChange(Dimmer, Dimmer.Switch, Bit.On);
                        }
                    }
                    else
                    {
                        X = Gen.Next(1, 3);
                        if (X == 1)
                        {
                            UnsolicitedTagStateChange(Dimmer, Dimmer.Dimmer, 25);
                        }
                        else
                        {
                            UnsolicitedTagStateChange(Dimmer, Dimmer.Dimmer, 75);
                        }
                    }
                }
                if (Device is TestLightSwitch)
                {
                    TestLightSwitch Switch = (TestLightSwitch)Device;
                    X = Gen.Next(1, 3);
                    if (X == 1)
                    {
                        UnsolicitedTagStateChange(Switch, Switch.Switch, Bit.Off);
                    }
                    else
                    {
                        UnsolicitedTagStateChange(Switch, Switch.Switch, Bit.On);
                    }
                }

                //simulate handling is async requests
                while (true)
                {
                    int count = 0;
                    lock (Requests)
                    {
                        count = Requests.Count;
                    }
                    Request4TagStateChange Request = null;
                    if (count >= 1)
                    {
                        lock (Requests)
                        {
                            Request = Requests.Dequeue();
                        }
                        //this.FinishStateChangeAsync(Request, true);
                        Request.Complete(true, true);
                    }
                    else
                    {
                        break;
                    }
                    Thread.Sleep(200);
                }
            }
        }

        protected override Type DeviceType
        {
            get { return typeof(TestDevice); }
        }

        //public override bool PerformTagStateChange(RequestTagStateChange Request)
        //{
        //    System.Threading.Thread.Sleep(250);
        //    Request.Complete(true);
        //    return true;
        //}

        public override void BeginRequest(Request4DeviceInterface Request)
        {
            if (Request is Request4TagStateChange)
            {
                BeginTagStateChange((Request4TagStateChange)Request);
            }
            if (Request is Request4TagState)
            {
                BeginTagStateReqest((Request4TagState)Request);
            }
        }       

        private void BeginTagStateChange(Request4TagStateChange Request)
        {
            lock (Requests)
            {
                this.Requests.Enqueue(Request);
            }
        }

        private void BeginTagStateReqest(Request4TagState Request)
        {
            throw new NotImplementedException();
        }
    }
}
